package com.oren.config;

import com.oren.annotation.Column;
import com.oren.annotation.Id;
import com.oren.annotation.Table;
import com.oren.constant.ConfigConstant;
import com.oren.util.ConnectionUtil;
import com.oren.util.EntityUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wsw
 * @time 2018/7/9 21:47
 * @description
 * @Since jdk1.7
 */
public final class DatabaseHelper {

	private final static String DRIVER;
	private final static String URL;
	private final static String USERNAME;
	private final static String PASSWORD;

	public static final QueryRunner QUERY_RUNNER;

	//利用dpcp2的对象去操作数据库
	//private static final BasicDataSource DATA_SOURCE;

	private final static Logger LOGGER= LoggerFactory.getLogger(DatabaseHelper.class);
	//局部线程变量，线程安全
	private static ThreadLocal<Connection> connContainer=new ThreadLocal<Connection>();

	static {
		QUERY_RUNNER=new QueryRunner();

		DRIVER=ConfigHelper.getJdbcDriver();
		URL=ConfigHelper.getJdbcUrl();
		USERNAME=ConfigHelper.getJdbcUsername();
		PASSWORD=ConfigHelper.getJdbPassword();

	/*	DATA_SOURCE=new BasicDataSource();
		DATA_SOURCE.setDriverClassName(DRIVER);
		DATA_SOURCE.setUrl(URL);
		DATA_SOURCE.setUsername(USERNAME);
		DATA_SOURCE.setPassword(PASSWORD);*/

	}

	/**
	 * 打开数据库连接
	 * @return
	 */
	public static Connection getConnection(){
		Connection connection=connContainer.get();
		try {
			if(connection == null){
				Class.forName(DRIVER);
				connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
			}
		}catch (Exception e){
			LOGGER.debug("connect database failure",e);
		}finally {
			connContainer.set(connection);
		}
		return connection;
	}

	/**
	 * 关闭数据库连接
	 */
	public static void closeConnection(){
		Connection connection=connContainer.get();
		try {
			if(connection!=null){
				connection.close();
			}
		}catch (Exception e){
			LOGGER.debug("close database failure",e);
		}finally {
			connContainer.remove();
		}
	}


	/**
	 * 开启事务
	 */
	public static void beginTransaction(){
		Connection conn=getConnection();
		if(conn!=null){
			try {
				conn.setAutoCommit(false);
			}catch (SQLException e){
				LOGGER.error("begin transaction failrue",e);
				throw new RuntimeException(e);
			}finally {
				connContainer.set(conn);
			}
		}
	}

	/**
	 * 提交事务
	 */
	public static void commitTransaction(){
		Connection connection=getConnection();
		if(connection != null){
			try {
				connection.commit();
				connection.close();
			}catch (SQLException e){
				LOGGER.error("commit transaction failure",e);
				throw  new RuntimeException(e);
			}finally {
				connContainer.remove();
			}
		}
	}

	/**
	 * 回滚事务
	 */
	public static void rollbackTransaction(){
		Connection connection=getConnection();
		if(connection != null){
			try {
				connection.rollback();
				connection.close();
			}catch (SQLException e){
				LOGGER.error("rollback transaction failure",e);
				throw  new RuntimeException(e);
			}finally {
				connContainer.remove();
			}

		}
	}





}
